package com.luojialong.onlineJudge.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luojialong.onlineJudge.common.BaseResponse;
import com.luojialong.onlineJudge.common.ErrorCode;
import com.luojialong.onlineJudge.common.ResultUtils;
import com.luojialong.onlineJudge.exception.BusinessException;
import com.luojialong.onlineJudge.model.dto.questionsubmit.QuestionSubmitAddRequest;
import com.luojialong.onlineJudge.model.dto.questionsubmit.QuestionSubmitQueryRequest;
import com.luojialong.onlineJudge.model.entity.QuestionSubmit;
import com.luojialong.onlineJudge.model.entity.User;
import com.luojialong.onlineJudge.model.vo.QuestionSubmitVO;
import com.luojialong.onlineJudge.otherPython.AIHelper;
import com.luojialong.onlineJudge.service.QuestionService;
import com.luojialong.onlineJudge.service.QuestionSubmitService;
import com.luojialong.onlineJudge.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


@RestController
@RequestMapping("/question_submit")
@Slf4j
@CrossOrigin(value = "http://localhost:8080")
public class QuestionSubmitController {

    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private UserService userService;

    @Resource
    private QuestionService questionService;

    /**
     * 点赞 / 取消点赞
     *
     * @param questionSubmitAddRequest
     * @param request
     * @return resultNum 本次点赞变化数
     */
    @PostMapping("/")
    public BaseResponse<QuestionSubmitVO> doQuestionSubmit(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest,
                                                           HttpServletRequest request) {
        if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能点赞
        final User loginUser = userService.getLoginUser(request);
        QuestionSubmit questionSubmit = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);

        return ResultUtils.success(QuestionSubmitVO.objToVo(questionSubmit));
    }


    /**
     * 分页获取题目提交列表（除管理员外,普通用户只能看到非答案,提交代码等公开信息）
     *
     * @param questionSubmitQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<QuestionSubmitVO>> listQuestionSubmitByPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest,
                                                                         HttpServletRequest request) {
        long current = questionSubmitQueryRequest.getCurrent();
        long size = questionSubmitQueryRequest.getPageSize();
        Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, size),
                questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(questionSubmitService.getQuestionVOPage(questionSubmitPage, loginUser));
    }


    @PostMapping("/analyzeCode")
    public BaseResponse<String> analyzeCode(String code) throws IOException, InterruptedException {
        String result = AIHelper.analyzeCode(code);
        return ResultUtils.success(result);
    }

}
